package com.xiaoyang.forum.service.impl;

import com.xiaoyang.forum.common.code.Result;
import com.xiaoyang.forum.common.code.ResultCode;
import com.xiaoyang.forum.common.exception.ApplicationException;
import com.xiaoyang.forum.domain.pojo.Article;
import com.xiaoyang.forum.domain.pojo.ArticleReply;
import com.xiaoyang.forum.domain.pojo.User;
import com.xiaoyang.forum.domain.vo.ArticleReplyUserVO;
import com.xiaoyang.forum.mapper.ArticleMapper;
import com.xiaoyang.forum.mapper.ArticleReplyMapper;
import com.xiaoyang.forum.mapper.UserMapper;
import com.xiaoyang.forum.service.IArticleReplyService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.List;

/**
 * @author XiaoYang
 * @since 2025-08-19
 */
@Slf4j
@Service
public class ArticleReplyServiceImpl extends ServiceImpl<ArticleReplyMapper, ArticleReply> implements IArticleReplyService {

    @Autowired
    private ArticleReplyMapper articleReplyMapper;

    @Autowired
    private ArticleMapper articleMapper;

    @Autowired
    private UserMapper userMapper;

    /**
     * 发布评论
     * @param userId 发布评论的用户id
     * @param articleId 文章id
     * @param content 评论内容
     */
    @Override
    public void create(long userId, Long articleId, String content) {
        log.info("发布评论");
        // 1. 校验文章是否存在
        Article article = extracted(articleId);

        // 2. 校验用户是否被禁言
        extracted(userId);

        // 3. 创建评论
        ArticleReply articleReply = new ArticleReply();
        articleReply.setPostUserId(Long.valueOf(userId)); // 发布评论的用户id
        articleReply.setArticleId(articleId); // 文章id
        articleReply.setContent(content); // 评论内容
        articleReply.setCreateTime(LocalDateTime.now());
        articleReply.setUpdateTime(LocalDateTime.now());

        // 4. 存储数据库
        articleReplyMapper.insert(articleReply);

        // 5. 更新文章评论数
        article.setReplyCount(article.getReplyCount() + 1);
        articleMapper.updateById(article);

    }

    /**
     * 回复评论
     * @param userId 回复评论的用户id
     * @param articleId 文章id
     * @param replyId 评论id
     * @param replyUserId 被回复的评论用户id
     * @param content 回复内容
     */
    @Override
    public void reply(long userId, Long articleId, Long replyId, Long replyUserId, String content) {
        log.info("回复评论");

        // 1. 校验文章是否存在
        Article article = extracted(articleId);

        // 2. 根据被回复的评论用户id和文章id校验评论是否存在
        ArticleReply articleReply = articleReplyMapper.selectById(replyId);
        if (articleReply == null) {
            log.info("评论不存在");
            throw new ApplicationException(Result.failed(ResultCode.FAILED_REPLY_NOT_EXIST));
        }

        // 2. 校验用户是否被禁言
        extracted(userId);

        // 3. 添加回复
        ArticleReply addUserReply = new ArticleReply();
        addUserReply.setArticleId(articleId);
        addUserReply.setPostUserId(userId);
        addUserReply.setReplyUserId(replyUserId);
        addUserReply.setContent(content);
        addUserReply.setReplyId(replyId);
        addUserReply.setCreateTime(LocalDateTime.now());
        addUserReply.setUpdateTime(LocalDateTime.now());
        articleReplyMapper.insert(addUserReply);

        // 5. 更新文章评论数
        article.setReplyCount(article.getReplyCount() + 1);
        articleMapper.updateById(article);
    }

    /**
     * 获取列表回复
     * @param articleId
     * @return
     */
    @Override
    public List<ArticleReplyUserVO> toList(Long articleId) {
        log.info("获取列表回复");
        if (articleId == null) {
            log.info("文章id不能为空");
            throw new ApplicationException(Result.failed(ResultCode.FAILED_NOT_EXISTS));
        }
        Article article = articleMapper.selectById(articleId);
        if (article == null) {
            // 文章不存在
            log.info("文章不存在" + articleId);
            throw new ApplicationException(Result.failed(ResultCode.FAILED_NOT_EXISTS));
        }

        return articleReplyMapper.toList(articleId);
    }

    /**
     * 校验用户是否被禁言
     * @param userId
     */
    private void extracted(long userId) {
        User user = userMapper.selectById(userId);
        if (user.getState() == 1) {
            // 用户被禁言
            log.info("用户被禁言" + userId);
            throw new ApplicationException(Result.failed(ResultCode.FAILED_USER_BANNED));
        }
    }

    /**
     * 校验文章是否存在
     * @param articleId
     */
    private Article extracted(Long articleId) {
        Article article = articleMapper.selectById(articleId);
        if (article == null) {
            // 文章不存在
            log.info("文章不存在" + articleId);
            throw new ApplicationException(Result.failed(ResultCode.FAILED_NOT_EXISTS));
        }
        return article;
    }
}
